package com.dzwww.recommend.rule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzwww.recommend.rule.common.api.ApiResult;
import com.dzwww.recommend.rule.common.po.RuleDispose;
import com.dzwww.recommend.rule.service.common.RuleDisposeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

/**
 * 规则配置管理api接口(新版)
 * @author Administrator
 * 2019年7月24日
 */
@RestSchema(schemaId = "ruleDispose")
@RequestMapping(path = "/api/ruleDispose/management", produces = MediaType.APPLICATION_JSON)
@Slf4j
public class RuleDisposeController  {

    @Resource
    RuleDisposeService ruleDisposeService;

    /**
     * 添加规则配置
     */
    @PostMapping
    public ApiResult managementSave(@RequestBody RuleDispose ruleDispose){
    	log.info("---managementSave--"+ruleDispose.toString());
    	Integer validDays = ruleDispose.getValidDays();
    	if(validDays != null){
    		int days = validDays.intValue();
    		Calendar cd = Calendar.getInstance();
    		ruleDispose.setEffectiveStarttime(cd.getTime());
    		cd.add(Calendar.DATE, days);
    		ruleDispose.setEffectiveEndtime(cd.getTime());
    	}
        boolean add = ruleDisposeService.add(ruleDispose);
        if(add){
            return ApiResult.succeed("添加成功");
        }
        return ApiResult.fail("","添加失败");
    }

    /**
     * 修改规则配置
     */
    @PutMapping
    public ApiResult managementUpdate(@RequestBody RuleDispose ruleDispose){
    	log.info("---managementUpdate--"+ruleDispose.toString());
    	Integer validDays = ruleDispose.getValidDays();
    	if(validDays != null){
    		int days = validDays.intValue();
    		RuleDispose rd = ruleDisposeService.getById(ruleDispose.getId());
    		Calendar cd = Calendar.getInstance();
    		cd.setTime(rd.getEffectiveStarttime());
    		cd.add(Calendar.DATE, days);
    		ruleDispose.setEffectiveEndtime(cd.getTime());
    	}
        boolean update = ruleDisposeService.updateById(ruleDispose);
        if(update){
            return ApiResult.succeed("修改成功");
        }
        return ApiResult.fail("","修改失败");
    }

    /**
     * 分页查询
     * @return      返回分页参数
     */
    @GetMapping
    public ApiResult managementPage(Integer current, Integer size, @RequestParam(value = "name",required = false) String name, 
    		@RequestParam(value = "type",required = false) Integer type){
    	log.info("---managementPage--"+name+type);
        if(current < 0){
            return ApiResult.fail("","起始页不能小于零");
        }
        Page<RuleDispose> page = new Page<RuleDispose>();
        page.setCurrent(current);
        page.setSize(size);
        page = ruleDisposeService.listPage(page, name, type);
        return ApiResult.succeed(page);
    }

    /**
     * 删除规则配置
     * @param id 删除的主键ID
     * @return 返回success
     */
    @DeleteMapping("/{id}")
    public ApiResult managementDelete(@PathVariable("id") Long id){
    	log.info("---managementDelete--"+id);
        boolean delete = ruleDisposeService.delById(id);
        if (delete){
            return ApiResult.succeed("删除成功");
        }
        return ApiResult.fail("","删除失败");
    }

    /**
     * 启用禁用
     * @param ruleDispose  修改参数
     * @return  返回success
     */
    @PatchMapping
    public ApiResult managementPatch(@RequestBody RuleDispose ruleDispose){
    	log.info("---managementPatch--"+ruleDispose.toString());
        boolean patch = ruleDisposeService.updateById(ruleDispose);
        if(patch){
            return ApiResult.succeed("操作成功");
        }
        return ApiResult.fail("","操作失败");
    }

    /**
     * 查询单个规则配置详情
     * @param id 唯一主键ID
     * @return  返回success
     */
    @GetMapping("/{id}")
    public ApiResult managementGetById(@PathVariable("id") Long id){
    	log.info("---managementGetById--"+id);
    	RuleDispose ruleDispose = ruleDisposeService.getById(id);
        return ApiResult.succeed(ruleDispose);
    }
    
    /**
     * 查询置顶的新闻ID列表
     * @param type
     * @return
     */
    @GetMapping("top")
    public ApiResult findList(){
    	log.info("---top--");
    	List<String> ruleList = new ArrayList<String>();
    	RuleDispose ruleDispose = new RuleDispose();
    	ruleDispose.setType(1);
    	ruleDispose.setStatus(1);
    	List<RuleDispose> list = ruleDisposeService.listBy(ruleDispose);
    	if(list != null && list.size() > 0){
    		Iterator<RuleDispose> it = list.iterator();
            while(it.hasNext()){
            	RuleDispose rd = (RuleDispose)it.next();
                if(rd.getValidDays() != null){
    				Calendar nowDate = Calendar.getInstance();
    				Calendar end = Calendar.getInstance();
    				end.setTime(rd.getEffectiveEndtime());
    				if(nowDate.after(end)) {
    					it.remove();
    				}
    			}
            }
    	}
    	if(list != null && list.size() > 0){
    		Set<String> set = new HashSet<String>();
    		for(RuleDispose rd : list){
    			String[] str = rd.getIds().split(",");
    			for(String s : str){
    				set.add(s.trim());
    			}
    		}
    		ruleList = new ArrayList<String>(set);
    	}
        return ApiResult.succeed(ruleList);
    }
    
    
}